<?php // no direct access
defined('_JEXEC') or die('Restricted access');
  
  $mainframe = JFactory::getApplication();
  if (!function_exists('ExtraFieldsToString')) {
    include('administrator/components/com_nspro/lib.php');
  }
  
  $myEmail = $_GET['u'];
  $nssource = $_GET['nss'];
  $myprecious = -1;

  $redirectToPage = $this->confirm_redirect;
  $redirectPage = $this->confirm_redir_page;
  $redirectMethod = $this->confirm_redir_method;

  $db =& JFactory::getDBO();

  $query = 'SELECT * FROM `#__nspro_subs` WHERE MD5(`email`) = "' . mysql_escape_string($myEmail) . '"';

  $db->setQuery( $query );
  $mSub = $db->loadObject();

  $myprecious = $mSub->id;

  if ($myprecious != -1) {
    if ($nssource == 'nsplgp') {
      jimport('joomla.application.plugin.helper');
      if ($this->j15 != true) {
        jimport( 'joomla.html.parameter' );
      }

      $plugin = & JPluginHelper::getPlugin('content', 'newsletter_subscriber_pro');

      $myParams = new JParameter($plugin->params);

      $recipient = $myParams->def('email_recipient', '');
      $subject   = $myParams->def('subject', 'New subscription to your site!');
      $fromName  = $myParams->def('from_name', 'Newsletter Subscriber');
      $fromEmail = $myParams->def('from_email', 'newsletter_subscriber@yoursite.com');
      $saveList  = $myParams->def('save_list', true);
      $savePath  = $myParams->def('save_path', 'mailing_list.txt');
      $saveFormat = $myParams->def('save_format', '%n (%e);');
      $pageText  = $myParams->def('page_text', 'Thank you for subscribing to our site.');
      $errorText = $myParams->def('errot_text', 'Your subscription could not be submitted. Please try again.');
      $myNameLabel = $myParams->def('name_label', 'Name:');
      $myEmailLabel = $myParams->def('email_label', 'Email:');
      $mailing_list_label = $myParams->def('mailing_list_label', 'Mailing Lists:');
      $thanksTextColor = $myParams->def('thank_text_color', '#FF0000');
      $errorTextColor = $myParams->def('error_text_color', '#FF0000');

      $sendingWithSetEmail = $myParams->def('sending_from_set', true);
    }
    else {
      jimport('joomla.application.module.helper');
      if ($this->j15 != true) {
        jimport( 'joomla.html.parameter' );
      }

      //$module = JModuleHelper::getModule( 'newsletter_subscriber_pro' );
      $db =& JFactory::getDBO();
      $db->setQuery('SELECT * FROM `#__modules` WHERE `module` = "mod_newsletter_subscriber_pro"');

      $module = $db->loadObject();

      $myParams = new JParameter($module->params);

      $recipient = $myParams->get('email_recipient', '');
      $subject   = $myParams->get('subject', 'New subscription to your site!');
      $fromName  = $myParams->get('from_name', 'Newsletter Subscriber');
      $fromEmail = $myParams->get('from_email', 'newsletter_subscriber@yoursite.com');
      $saveList  = $myParams->get('save_list', true);
      $savePath  = $myParams->get('save_path', 'mailing_list.txt');
      $saveFormat = $myParams->get('save_format', '%n (%e);');
      $pageText  = $myParams->get('page_text', 'Thank you for subscribing to our site.');
      $errorText = $myParams->get('errot_text', 'Your subscription could not be submitted. Please try again.');
      $myNameLabel = $myParams->get('name_label', 'Name:');
      $myEmailLabel = $myParams->get('email_label', 'Email:');
      $mailing_list_label = $myParams->get('mailing_list_label', 'Mailing Lists:');
      $thanksTextColor = $myParams->get('thank_text_color', '#FF0000');
      $errorTextColor = $myParams->get('error_text_color', '#FF0000');

      $sendingWithSetEmail = $myParams->get('sending_from_set', true);

    }

    if ($fromName == 'Newsletter Subscriber') {
      $fromName = $mainframe->getCfg('fromname');
    }
    if ($fromEmail == 'newsletter_subscriber@yoursite.com') {
      $fromEmail = $mainframe->getCfg('mailfrom');
    }

    $db =& JFactory::getDBO();
    $query = 'UPDATE `#__nspro_subs` SET `confirmed` = "1" WHERE `id` = "'.mysql_escape_string($myprecious).'"';
    $db->setQuery($query);
    if (!$db->query()) {
      print JText::_('Database Update Error. Please try again');
      return;
    }
    else {

      if (is_null($mSub)) {
        $query = 'SELECT * FROM `#__nspro_subs` WHERE `id` = "'.mysql_escape_string($myprecious).'"';
        $db->setQuery($query);
        $mSub = $db->loadObject();
      }

      $mailSender = &JFactory::getMailer();
      $mailSender->addRecipient($recipient);
      if ($sendingWithSetEmail) {
        $mailSender->setSender(array($fromEmail,$fromName));
      }
      else {
        $mailSender->setSender(array($mSub->email,$mSub->name));
      }
      $mailSender->setSubject($subject);

      $myMessage = $myNameLabel . ' ' . $mSub->name . ', ' .
                   $myEmailLabel . ' ' . $mSub->email . ', ' .
                   date("r") . ', ' .
                   $mailing_list_label . ' ';
      $alllists = explode(',', $mSub->mailing_lists);
      $myliststring = '';
      if (count($alllists) > 0) {
        foreach($alllists as $alist) {
          if ($alist != '') {
            $query = 'SELECT * FROM `#__nspro_lists` WHERE `published` = 1 and `id` = "' . mysql_escape_string($alist) . '"';
            $db->setQuery($query);
            $tlist = $db->loadObject();
            if ($myliststring == '') {
              $myliststring = $myliststring . $tlist->lname;
            }
            else {
              $myliststring = $myliststring . ', ' . $tlist->lname;
            }
          }
        }
      }
      $myMessage = $myMessage . $myliststring;
      
      $myMessage .= "\n" . ExtraFieldsToString('confirm', $mSub->id);

      $mailSender->setBody($myMessage);

      if (!$mailSender->Send()) {
        $myReplacement = '<span style="color: '.$errorTextColor.';">' . $errorText . '</span>';
        print $myReplacement;
      }
      else {
        if (!$redirectToPage) {
          $myReplacement = '<span style="color: '.$thanksTextColor.';">' . $pageText . '</span>';
          print $myReplacement;
        }
        else {
          if ($redirectMethod == 0) {
            header("Location: ".$redirectPage);
          }
          else {
            ?>
            <script type="text/javascript">
            <!--
            window.location = "<?php print $redirectPage;?>";
            //-->
            </script>
            <?php
          }
        }
      }
      if ($saveList) {
          $file = fopen($savePath, "a");
          if ($file !== false) {
            $mstring = JString::str_ireplace('%n', $mSub->name, $saveFormat);
            $mstring = JString::str_ireplace('%e', $mSub->email, $mstring);
            fwrite($file, $mstring);
            fclose($file);
          }
      }
    }
  }
  else {
    print JText::_('This link has expired');
  }


?>
